package com.mes.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mes.cloud.dto.BcViewDTO;
import com.mes.cloud.dto.BmeBcDTO;
import com.mes.cloud.dto.BmeBcFunDTO;
import com.mes.cloud.service.BmeBcFunService;
import com.mes.cloud.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * bme_bc_fun基础表
 *
 * @author xhy
 * @version 1.0
 * @date 2019/05/09 16:00
 */
@Service
public class BmeBcFunServiceImpl implements BmeBcFunService {
    @Autowired
    private com.mes.cloud.dao.BmeBcFunDAO bmeBcFunDAO;

    /**
     * 根据条件获取bme_bc_fun基础表列表
     *
     * @param condition
     * @return List<BmeBcFunDTO>
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public List<BmeBcFunDTO> getBmeBcFunList(Map<Object, Object> condition) {
        return bmeBcFunDAO.getBmeBcFunList(condition);
    }

    /**
     * 获取bme_bc_fun基础表的最大记录数
     *
     * @param condition
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long getBmeBcFunCount(Map<Object, Object> condition) {
        return bmeBcFunDAO.getBmeBcFunCount(condition);
    }

    /**
     * 新增bme_bc_fun基础表
     *
     * @param BmeBcFunDTO bme_bc_fun基础表对象]
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long insertBmeBcFun(BmeBcFunDTO BmeBcFunDTO) {
        BmeBcFunDTO.setId(UuId.getShortUuid());
        return bmeBcFunDAO.insertBmeBcFun(BmeBcFunDTO);
    }

    /**
     * 更新bme_bc_fun基础表
     *
     * @param BmeBcFunDTO bme_bc_fun基础表对象]
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long updateBmeBcFun(BmeBcFunDTO BmeBcFunDTO) {
        return bmeBcFunDAO.updateBmeBcFun(BmeBcFunDTO);
    }

    /**
     * 删除bme_bc_fun基础表
     *
     * @param id bme_bc_fun基础表标识]
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long delBmeBcFunById(String id) {
        return bmeBcFunDAO.delBmeBcFunById(id);
    }

    /**
     * 根据bme_bc_fun基础表标识获取bme_bc_fun基础表
     *
     * @param id bme_bc_fun基础表标识]
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public BmeBcFunDTO getBmeBcFunById(String id) {
        return bmeBcFunDAO.getBmeBcFunById(id);
    }

    @Override
    public CommPager<BmeBcFunDTO> findPageList(Map<Object, Object> condition) {
        int pageIndex = condition.get("pageIndex") == null ? 0 : Integer.valueOf(condition.get("pageIndex").toString());
        int pageSize = condition.get("pageSize") == null ? 10 : Integer.valueOf(condition.get("pageSize").toString());
        Page<BmeBcFunDTO> page = PageHelper.startPage(pageIndex, pageSize, true);
        List<BmeBcFunDTO> dictList = bmeBcFunDAO.getBmeBcFunList(condition);
        CommPager<BmeBcFunDTO> pageResult = new CommPager<>(page, dictList);
        return pageResult;
    }

    /**
     * 批量删除
     *
     * @param map
     * @return
     */
    @Override
    public Long batchDelete(Map<Object, Object> map) throws Exception {
        if (map.get("ids") == null) {
            throw new Exception("ids不能为空");
        }
        return bmeBcFunDAO.batchDelete((List<String>) map.get("ids"));
    }

    @Override
    public long enable(Map<Object, Object> map) throws Exception {
        if (map.get("id") == null || map.get("isEnable") == null) {
            throw new Exception("参数异常");
        }
        return bmeBcFunDAO.enable(map);
    }

    @Override
    public Long batchEnable(Map<Object, Object> map) throws Exception {
        if (map.get("ids") == null) {
            throw new Exception("ids不能为空");
        }
        return bmeBcFunDAO.batchEnable((List<Long>) map.get("ids"));
    }

    @Override
    public int importExcel(MultipartFile file) throws Exception {
        String rooPath = this.getClass().getResource("/").getPath();
        //复制文件到static目录
        String path = rooPath + "/static";
        String fileName = FileUploadUtils.uploadFile(file, path);
        String filePath = FileUploadUtils.uploadFile(file, path, fileName);
        FileInputStream fis = null;
        fis = new FileInputStream(filePath);
        // 创建工具类.
        ExcelUtil<BmeBcFunDTO> util = new ExcelUtil<BmeBcFunDTO>(BmeBcFunDTO.class);
        List<BmeBcFunDTO> paramList = util.importExcel(fileName, fis);
        HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
        if (StringUtils.isEmpty(paramList)) {
            throw new Exception("数据为空！");
        }
        for (BmeBcFunDTO bmeBcFunDTO : paramList) {
            if (StringUtils.isEmpty(bmeBcFunDTO.getId())) {
                bmeBcFunDTO.setId(UuId.getShortUuid());
            } else {
                objectObjectHashMap.put("id", bmeBcFunDTO.getId());
                long count = bmeBcFunDAO.getBmeBcFunCount(objectObjectHashMap);
                if (count > 0) {
                    throw new Exception("数据已经存在！");
                }
            }
        }
        return bmeBcFunDAO.insertExcel(paramList);
    }

    @Override
    public void exportFile(Map<Object, Object> map, HttpServletResponse response) throws Exception {
        List<BmeBcFunDTO> list = bmeBcFunDAO.getBmeBcFunList(map);
        if (list.size() == 0) {
            throw new Exception("导出数据为空！");
        }
        // 创建工具类
        ExcelUtil<BmeBcFunDTO> util = new ExcelUtil<BmeBcFunDTO>(BmeBcFunDTO.class);
        // 导出
        String topName = DateUtil.getDateTime("yyyyMMddHHmmss");
        util.exportFile(list, DateUtil.getDateTime(topName), topName, response);
    }
}
